package com.tpay.client.view;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.tpay.client.G;
import com.tpay.client.presenter.LoginPagePresenter;
import com.tpay.client.uihandlers.LoginPageUiHandlers;
import com.tpay.client.util.FieldVerifier;

public class LoginPageView extends ViewWithUiHandlers<LoginPageUiHandlers> implements LoginPagePresenter.MyView {
	private final VLayout layout;
	private final TextItem idItem;
	private final PasswordItem pwdItem;
	
	public LoginPageView() {
		Window.setMargin("0px");
		Window.enableScrolling(false);
		
		layout = new VLayout();
		layout.setWidth100();
		layout.setHeight100();
		layout.setAlign(Alignment.CENTER);				// 세로 가운데
		layout.setDefaultLayoutAlign(Alignment.CENTER);	// 가로 가운데
		
		DynamicForm form = new DynamicForm();
		form.setWidth(400);
		form.setAutoHeight();
		form.setNumCols(3);
		
		idItem = new TextItem();
		idItem.setRequired(true);
		idItem.setHint("이메일 입력");
		idItem.setDefaultValue("admin@time.com");
		idItem.setShowHintInField(true);
		idItem.setShowTitle(false);
		idItem.setSelectOnFocus(true);
		
		pwdItem = new PasswordItem();
		pwdItem.setRequired(true);
		pwdItem.setHint("비밀번호");
		pwdItem.setDefaultValue("1234");
		pwdItem.setShowHintInField(true);
		pwdItem.setShowTitle(false);
		idItem.setSelectOnFocus(true);
		
		ButtonItem okItem = new ButtonItem("로그인");
		okItem.setStartRow(false);
		okItem.setWidth("100%");
		
		form.setFields(idItem, pwdItem, okItem);
		layout.addMember(form);
		
		okItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String id = idItem.getValueAsString();
				String pwd = idItem.getValueAsString();
				
				if(!FieldVerifier.isValidId(id) || !FieldVerifier.isValidPassword(pwd)) {
					event.getForm().cancel();
					G.ShowMessage("아이디와 비밀번호를 바르게 입력하세요.", true);
					resetAndFocus();
					return;
				}
					
				if(getUiHandlers() != null)
					getUiHandlers().onLoginButtonClicked();
			}
		});
	}
	
	@Override
	public Widget asWidget() {
		return layout;
	}

	@Override
	public String getId() {
		return idItem.getValueAsString();
	}

	@Override
	public String getPassword() {
		return pwdItem.getValueAsString();
	}

	@Override
	public void resetAndFocus() {
		idItem.focusInItem();
	}

	@Override
	public void say(Throwable e) {
		SC.say(e.getMessage() + "\n" + e.getLocalizedMessage());
	}
}
